const express = require("express");
const expressJoi = require("@escook/express-joi");

// 通用路由注册函数
const registerRoutes = (router, moduleName) => {
	const { Login, Register } = require("../../router_handler/public/login");
	const { login_Schema, register_Schema } = require("../../schema/public/login");

	const routes = [
		{ method: "post", path: "/login", schema: login_Schema, handler: Login },
		{ method: "post", path: "/register", schema: register_Schema, handler: Register },
	];

	routes.forEach(route => {
		const handlerWithTable = (req, res) => route.handler(req, res, moduleName);
		if (route.schema) {
			router[route.method](route.path, expressJoi(route.schema), handlerWithTable);
		} else {
			router[route.method](route.path, handlerWithTable);
		}
	});
};

module.exports = function createModuleRouter(moduleName) {
	const router = express.Router();
	registerRoutes(router, moduleName);
	return router;
};